package cn.zyb.org.controller;

import cn.zyb.basic.util.AjaxResult;
import cn.zyb.basic.util.PageList;
import cn.zyb.org.domain.Department;
import cn.zyb.org.query.DepartmentQuery;
import cn.zyb.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门接口类",description = "部门接口的CRUD操作")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：查询所有
     * @return
     */
    // http://localhost:8080/department - Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：查询单个对象
     * @return
     */
    // http://localhost:8080/department/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }

    /**
     * 分页接口
     * @param departmentQuery
     * @return
     */
    // http://localhost:8080/department - Post - 参数通过json对象传递
    @PostMapping
    //@RequestBody -从请求体中获取数据赋值给departmentQuery
    @ApiOperation(value = "分页接口")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    // http://localhost:8080/department/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 接口：添加和修改
      * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){//添加
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

  /**
   * 接口：批量删除
   * @param ids
   * @return
   */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
 public AjaxResult patchDel(@RequestBody List<Long> ids){
   try {
     departmentService.patchDel(ids);
     return new AjaxResult();
   } catch (Exception e) {
     e.printStackTrace();
     return new AjaxResult(false,"删除失败");
   }
 }

  /**
   * 接口：部门数
   * @return
   */
  @GetMapping("/deptTree")
  @ApiOperation(value = "查询部门数--无限极")
  public List<Department> deptTree() {
    return departmentService.deptTree();
  }


}
